r = []
s = []

SEARCH_NUM  = '128'
LAB1_START  = 100
LAB2_START  = 200
LAB3_START  = 300
LAB4_START  = 400
LAB5_INTER  = 600
LAB5_DIFF   = 650
LAB5_UNION  = 700

def readData(start, size):
    arr = []
    for i in range(start, start + size):
        with open(f"./data/{i}.blk", 'rb') as f:
            for _ in range(7):
                str1 = f.read(4).decode().replace('\x00', '')
                str2 = f.read(4).decode().replace('\x00', '')
                if str1 == '':
                    break
                arr.append((str1, str2))
    return arr

def readSource():
    global r, s
    r = readData(1, 16)
    s = readData(17, 32)
    print(len(r), len(s))          

def lab1():
    print('*'*40, 'lab1', '*'*40,)
    arr1 = []
    for t in s:
        if t[0] == SEARCH_NUM:
            arr1.append(t)
    arr2 = readData(LAB1_START, 2)
    print(f"理论组数: {len(arr1)}, 实际组数: {len(arr2)}")
    print('比较集合是否相等: ', set(arr1) == set(arr2))
    
def lab2():
    print('*'*40, 'lab2', '*'*40,)
    arr = readData(LAB2_START, 16)
    print('检查集合是否相等: ', set(arr) == set(r))
    flag = True
    pre = arr[0][0]
    for t in arr:
        if t[0] < pre:
            flag = False
            break
    print('检查是否按顺序: ', flag)

    
    arr = readData(LAB2_START + 16, 32)
    print('检查集合是否相等: ', set(arr) == set(s))
    flag = True
    pre = arr[0][0]
    for t in arr:
        if t[0] < pre:
            flag = False
            break
    print('检查是否按顺序: ', flag)

def lab3():
    print('*'*40, 'lab3', '*'*40,)
    arr1 = []
    for t in s:
        if t[0] == SEARCH_NUM:
            arr1.append(t)
    arr2 = readData(LAB3_START, 2)
    print(f"理论组数: {len(arr1)}, 实际组数: {len(arr2)}")
    print('比较集合是否相等: ', set(arr1) == set(arr2))

def lab4():
    print('*'*40, 'lab4', '*'*40,)
    arr = []
    for i in r:
        for j in s:
            if i[0] == j[0]:
                arr.append((j, i))
    arr2 = readData(LAB4_START, 112)
    print('read from disk: ', len(arr2))
    arr3 = []
    for i in range(0, len(arr2), 2):
        arr3.append((arr2[i], arr2[i + 1]))
    print(f"理论组数: {len(arr)}, 实际组数: {len(arr3)}")
    print('比较集合是否相等', set(arr) == set(arr3))

def lab5():
    print('*'*40, 'lab5', '*'*40,)
    arr1 = set(r) & set(s) #交
    arr2 = set(r) | set(s) #并
    arr3 = set(r) - set(s) #差 R-S
    # arr4 = set(s) - set(r) #差 S-R

    print('交: ')
    arr = readData(LAB5_INTER, 2)
    print(f"理论组数: {len(arr1)}, 实际组数: {len(arr)}")
    print('比较集合是否相等', set(arr) == set(arr1))

    print('并: ')
    arr = readData(LAB5_UNION, 47)
    print(f"理论组数: {len(arr2)}, 实际组数: {len(arr)}")
    print('比较集合是否相等', set(arr) == set(arr2))

    print('差: ')
    arr = readData(LAB5_DIFF, 15)
    print(f"理论组数: {len(arr3)}, 实际组数: {len(arr)}")
    print('比较集合是否相等', set(arr) == set(arr3))

    # print("差2")
    # arr = readData(800, 31)
    # print(f"理论组数: {len(arr4)}, 实际组数: {len(arr)}")
    # print('比较集合是否相等', set(arr) == set(arr4))

    # print(set(arr3) - set(arr))


def main():
    readSource()
    lab1()
    lab2()
    lab3()
    lab4()
    lab5()

if __name__ == '__main__' :
    main()